package com.jack.student.controller;

import com.jack.student.dao.StudentDao;
import com.jack.student.entity.Student;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class StudentController {
    //dao层
    private StudentDao studentDao = new StudentDao();

    //获取用户输入
    private Scanner scanner = new Scanner(System.in);

    //登录
    public void login(){
        System.out.println("----登录界面----");
        System.out.println("请输入用户名：");
        String s1 = scanner.next();
        System.out.println("请输入密码：");
        String s2 = scanner.next();
        if ("lisi".equals(s1) && "8888".equals(s2)){
            System.out.println("登录成功！");
        }else {
            System.out.println("用户名或密码不正确！");
            System.exit(0);
        }
    }

    //主界面
    public void show(){
        System.out.println("1、统计学生人数");
        System.out.println("2、查看学生名单");
        System.out.println("3、按照学号查询学生姓名");
        System.out.println("4、按照姓名查询学生信息");
        System.out.println("5、修改学生出生日期");
        System.out.println("6、删除学生记录");
        System.out.println("7、退出");
    }

    //用户的选择
    public void select(Integer num) throws SQLException, ParseException {
        switch (num){
            case 1:
                this.count();
                break;
            case 2:
                this.allStudent();
                break;
            case 3:
                this.findNameByID();
                break;
            case 4:
                this.findByname();
                break;
            case 5:
                this.updateData();
                break;
            case 6:
                this.deleteById();
                break;
            case 7:
                System.exit(0);

            default:
                System.out.println("输入错误");
        }
    }

    //统计人数
    public void count() throws SQLException {
        Object count = studentDao.stuCount();
        System.out.println("学生人数为："+count);
    }

    //查看学生名单
    public void allStudent() throws SQLException {
        List<Student> list = studentDao.findAll();
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //按照学号查询学生姓名
    public void findNameByID() throws SQLException {
        System.out.println("请输入学生学号：");
        String id = scanner.next();
        String name = studentDao.findNameByID(id);
        System.out.println("学生姓名为："+name);
    }

    //按照姓名查询学生信息
    public void findByname() throws SQLException {
        System.out.println("请输入学生的姓名：");
        String name = scanner.next();
        Student student = studentDao.findOneByName(name);
        System.out.println("学生信息为："+student);
    }

    //修改学生出生日期
    public void updateData() throws ParseException, SQLException {
        System.out.println("请输入学生姓名：");
        String name = scanner.next();
        System.out.println("请输入学生出生日期:");
        String time = scanner.next();
        // 将用户的输入转成Date
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        Date newDate = simpleDateFormat.parse(time);

        int index = studentDao.updateStuBirthday(newDate);
        if (index>0){
            System.out.println("日期更改成功");
        }else {
            System.out.println("日期更改失败");
        }


    }
    //删除学生记录
    public void deleteById() throws SQLException {
        System.out.println("请输入学生的姓名：");
    String name = scanner.next();
    int index = studentDao.deleteStuByName(name);
        if (index>0){
        System.out.println("删除成功");
    }else {
        System.out.println("删除失败");
    }
}



    //主函数，测试所有的功能，已完成
    public static void main(String[] args) throws SQLException, ParseException {
        StudentController controller = new StudentController();
        controller.login();
        controller.show();
        System.out.println("请输入编号：");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        controller.select(i);

    }
}
